﻿using System;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Kiva7.Controls
{
    public class PhonePage : PhoneApplicationPage
    {
        #region Orientation

        public PhonePage()
        {
            this.Loaded += PhonePageLoaded;
        }

        private void PhonePageLoaded(object sender, RoutedEventArgs e)
        {
            try
            {
                SystemTray.IsVisible = false;
            }
            catch (InvalidOperationException)
            {
            }
            
            ProcessOrientation(Orientation);
        }

        protected override void OnOrientationChanged(OrientationChangedEventArgs e)
        {
            base.OnOrientationChanged(e);
            ProcessOrientation(e.Orientation);
        }

        #region Width

        public static readonly DependencyProperty WidthLProperty = DependencyProperty.RegisterAttached(
            "WidthL", //Name of the property
            typeof (double?), //Type of the property
            typeof (PhonePage), //Type of the provider of the registered attached property
            null); //Callback invoked in case the property value has changed


        public static void SetWidthL(DependencyObject obj, double? WidthL)
        {
            obj.SetValue(WidthLProperty, WidthL);
        }

        public static double? GetWidthL(DependencyObject obj)
        {
            return (double?) obj.GetValue(WidthLProperty);
        }


        public static readonly DependencyProperty WidthPProperty = DependencyProperty.RegisterAttached(
    "WidthP ", //Name of the property
    typeof(double?), //Type of the property
    typeof(PhonePage), //Type of the provider of the registered attached property
    null); //Callback invoked in case the property value has changed


        public static void SetWidthP(DependencyObject obj, double? WidthP)
        {
            obj.SetValue(WidthPProperty, WidthP);
        }

        public static double? GetWidthP(DependencyObject obj)
        {
            return (double?)obj.GetValue(WidthPProperty);
        }

        #endregion

        #region Height

        public static readonly DependencyProperty HeightLProperty = DependencyProperty.RegisterAttached(
            "HeightL", //Name of the property
            typeof(double?), //Type of the property
            typeof(PhonePage), //Type of the provider of the registered attached property
            null); //Callback invoked in case the property value has changed


        public static void SetHeightL(DependencyObject obj, double? HeightL)
        {
            obj.SetValue(HeightLProperty, HeightL);
        }

        public static double? GetHeightL(DependencyObject obj)
        {
            return (double?)obj.GetValue(HeightLProperty);
        }


        public static readonly DependencyProperty HeightPProperty = DependencyProperty.RegisterAttached(
    "HeightP ", //Name of the property
    typeof(double?), //Type of the property
    typeof(PhonePage), //Type of the provider of the registered attached property
    null); //Callback invoked in case the property value has changed


        public static void SetHeightP(DependencyObject obj, double? HeightP)
        {
            obj.SetValue(HeightPProperty, HeightP);
        }

        public static double? GetHeightP(DependencyObject obj)
        {
            return (double?)obj.GetValue(HeightPProperty);
        }

        #endregion

        #region Visibility

        public static readonly DependencyProperty VisibilityLProperty = DependencyProperty.RegisterAttached(
            "VisibilityL", //Name of the property
            typeof(Visibility?), //Type of the property
            typeof(PhonePage), //Type of the provider of the registered attached property
            null); //Callback invoked in case the property value has changed


        public static void SetVisibilityL(DependencyObject obj, Visibility? visibilityL)
        {
            obj.SetValue(VisibilityLProperty, visibilityL);
        }

        public static Visibility? GetVisibilityL(DependencyObject obj)
        {
            return (Visibility?)obj.GetValue(VisibilityLProperty);
        }


        public static readonly DependencyProperty VisibilityPProperty = DependencyProperty.RegisterAttached(
    "VisibilityP ", //Name of the property
    typeof(Visibility?), //Type of the property
    typeof(PhonePage), //Type of the provider of the registered attached property
    null); //Callback invoked in case the property value has changed


        public static void SetVisibilityP(DependencyObject obj, Visibility? visibilityP)
        {
            obj.SetValue(VisibilityPProperty, visibilityP);
        }

        public static Visibility? GetVisibilityP(DependencyObject obj)
        {
            return (Visibility?)obj.GetValue(VisibilityPProperty);
        }

        #endregion


        #region Process

        //Just a sample method ilustrating the idea how to obtain the value

        private void ProcessOrientation(PageOrientation orientation)
        {
            ProcessChildrenOrientation(this, orientation);
        }

        private void ProcessChildrenOrientation(DependencyObject control, PageOrientation orientation)
        {
            ProcessOrientation(control as FrameworkElement, orientation);
            for(int i = 0, j = VisualTreeHelper.GetChildrenCount(control); i < j; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(control, i); 
                ProcessChildrenOrientation(child, orientation);
            }
        }

        private void ProcessOrientation(FrameworkElement control, PageOrientation orientation)
        {
            if (control == null)
            {
                return;
            }
            if ((orientation & PageOrientation.Landscape) == PageOrientation.Landscape)
            {
                SetAsLandscape(control);
            }
            else
            {
                SetAsPortrait(control);
            }
        }

        private static void SetAsPortrait(FrameworkElement control)
        {
            double? WidthP = PhonePage.GetWidthP(control);
            if (WidthP.HasValue)
            {
                control.Width = WidthP.Value;
            }

            double? HeightP = PhonePage.GetHeightP(control);
            if (HeightP.HasValue)
            {
                control.Height = HeightP.Value;
            }

            Visibility? VisibilityP = PhonePage.GetVisibilityP(control);
            if (VisibilityP.HasValue)
            {
                control.Visibility = VisibilityP.Value;
            }

        }

        private static void SetAsLandscape(FrameworkElement control)
        {
            double? WidthL = PhonePage.GetWidthL(control);
            if (WidthL.HasValue)
            {
                control.Width = WidthL.Value;
            }

            double? HeightL = PhonePage.GetHeightL(control);
            if (HeightL.HasValue)
            {
                control.Height = HeightL.Value;
            }

            Visibility? visibility = PhonePage.GetVisibilityL(control);
            if (visibility.HasValue)
            {
                control.Visibility = visibility.Value;
            }

        }

        #endregion

        #endregion
    }
}
